/*eslint-disable*/
import { Moment } from 'moment';
import {
  getAppListByTenantCode,
  getComponentListByAppId,
  getTenantList,
} from '/@/api/maintenance-center/system-log/systemLogs';

export const columns = [
  {
    title: '主机IP',
    dataIndex: 'hostIP',
    width: 100,
  },
  {
    title: 'Pod名称',
    dataIndex: 'podName',
    width: 100,
  },
  {
    title: '日志内容',
    dataIndex: 'content',
    width: 100,
  },
  {
    title: '发生时间',
    dataIndex: 'time',
    width: 100,
  },
];

export const searchFormSchema = [
  {
    field: 'limit',
    label: '日志条数',
    required: true,
    component: 'Input',
    defaultValue: '5',
    rules: [
      {
        trigger: 'change',
        required: true,
        message: '请输入日志条数',
      },
      {
        trigger: 'change',
        pattern: /^(?:[1-9]|\d{2,3}|[1-4]\d{3}|5000)$/,
        message: '请输入不大于5000条数',
      },
    ],
    componentProps: {
      placeholder: '请输入日志条数',
    },
    labelWidth: 100,
    colProps: {
      span: 6,
    },
  },
  {
    field: 'logTime',
    label: '发生时间',
    component: 'RangePicker',
    required: true,
    componentProps: {
      placeholder: '请选择发生时间',
      showTime: true,
      disabledDate: (current: Moment) => {
        const dates = new Date().toLocaleDateString();
        const diffDate = current.diff(dates, 'days');
        return diffDate < -30 || diffDate > 0;
      },
    },
    labelWidth: 100,
    colProps: {
      span: 6,
    },
  },
  {
    field: 'keyword',
    label: '关键词',
    component: 'Input',
    componentProps: {
      placeholder: '请输入关键词',
    },
    labelWidth: 100,
    colProps: {
      span: 6,
    },
  },
  {
    field: 'tenantCode',
    label: '租户',
    component: 'ApiSelect',
    required: true,
    componentProps: ({ formActionType }) => {
      return {
        placeholder: '请选择租户',
        api: getTenantList,
        resultField: 'list',
        labelField: 'name',
        valueField: 'code',
        onChange: async (e) => {
          const { setFieldsValue, updateSchema } = formActionType;

          setFieldsValue({
            appId: undefined,
            appName: undefined,
            componentId: undefined,
            componentName: undefined,
          });
          updateSchema({
            field: 'appId',
            label: '应用',
            component: 'ApiSelect',
            required: true,
            dynamicDisabled: e ? false : true,
            labelWidth: 100,
            colProps: {
              span: 6,
            },
            componentProps: ({ formActionType }) => {
              return {
                placeholder: '请选择应用',
                api: getAppListByTenantCode,
                immediate: true,
                params: {
                  tenantCode: e,
                },
                resultField: 'list',
                labelField: 'appName',
                valueField: 'appId',
                onChange: () => {
                  const { setFieldsValue, updateSchema } = formActionType;
                  setFieldsValue({
                    componentId: undefined,
                    componentName: undefined,
                  });
                  updateSchema({
                    field: 'componentId',
                    required: true,
                  });
                },
              };
            },
          });
        },
      };
    },
    labelWidth: 100,
    colProps: {
      span: 6,
    },
  },
  {
    field: 'appId',
    label: '应用',
    component: 'ApiSelect',
    dynamicDisabled: true,
    labelWidth: 100,
    colProps: {
      span: 6,
    },
  },
  {
    field: 'componentId',
    label: '组件',
    component: 'ApiSelect',
    dynamicDisabled: ({ values }) => {
      return values.appId && values.tenantCode ? false : true;
    },
    componentProps: ({ formModel }) => {
      return {
        api: getComponentListByAppId,
        immediate: false,
        params: {
          appId: formModel.appId,
        },
        resultField: 'list',
        labelField: 'componentName',
        valueField: 'componentId',
        placeholder: '请选择组件',
      };
    },
    labelWidth: 100,
    colProps: {
      span: 6,
    },
  },
];
